package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.AddressVo;
import com.itheima.service.AddressService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    //查询所有的地址信息(分页查询)
    @Override
    public PageResult findAllAddressByPage(QueryPageBean param) {
        //获取分页的相关信息
        Integer currentPage = param.getCurrentPage();//当前页
        Integer pageSize = param.getPageSize();//每页显示条数
        //调用pagehelper
        PageHelper.startPage(currentPage,pageSize);
        //获取查询条件
        String queryString = param.getQueryString();
        //分页查询一定要返回Page类(pageHelper要求)
        Page<Address> page= addressDao.findAddressListByQuery(queryString);
        long total = page.getTotal();//总条数
        List<Address> rows = page.getResult();//当前页的数据
        //封装到返回值
        return new PageResult(total,rows);


    }
    //添加地址
    @Override
    public void add(Address address) {
        addressDao.add(address);
    }
    //保存修改后的地址
    @Override
    public void edit(Address address) {
        addressDao.edit(address);
    }
    //删除通过id
    @Override
    public void deleteById(Integer id) {
        addressDao.deleteById(id);
    }
    //获取标点数据
    @Override
    public List<AddressVo> getAllAddress() {
        List<Address> addressList= addressDao.getAllAddress();
        //封装数据
        AddressVo addressVo = null;
        List<AddressVo> addressVoList =  new ArrayList<AddressVo>();
        for (Address address : addressList) {
            addressVo = new AddressVo();
            addressVo.setName(address.getName());
            addressVo.setAddressDetails(address.getAddressDetails());
            String coordinate = address.getCoordinate();
            if(coordinate != null && coordinate.length() > 0){
                //分割经纬度
                String[] split = coordinate.split(",");
                String lon = split[0];
                String lat = split[1];
                addressVo.setLongt(lon);//经度
                addressVo.setLat(lat);//纬度
            }
            System.out.println(addressVo);
            addressVoList.add(addressVo);
        }
        return addressVoList;
        //封装数据
//        AddressVo addressVo = new AddressVo();
//        List<AddressVo> list = addressList.stream().map(address -> {
//            addressVo.setName(address.getName());
//            String coordinate = address.getCoordinate();
//            if(coordinate != null && coordinate.length() > 0){
//                //分割经纬度
//                String[] split = coordinate.split(",");
//                String lon = split[0];
//                String lat = split[1];
//                addressVo.setLongt(lon);//经度
//                addressVo.setLat(lat);//纬度
//            }
//            System.out.println(addressVo);
//            return addressVo;
//        }).collect(Collectors.toList());
//        System.out.println(list);
//        return list;
    }

    @Override
    public List<String> getAddressNames() {
        return addressDao.getAddressNames();
    }
}
